package com.users.controller;

import com.alibaba.fastjson2.JSONObject;
import com.common.common.BaseResponse;
import com.common.common.ResultUtils;
import com.common.controller.AbstractTypedController;
import com.users.entities.User;
import com.users.feign.OrderFeignService;
import com.users.feign.StoreFeignService;
import com.users.service.UserServiceImpl;
import com.users.vo.Register;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author fu
 */
@Component
@RestController
@RequestMapping("user")
@Slf4j
@CrossOrigin
@Tag(name = "用户表")
public class UserController extends AbstractTypedController<User, Long> {

    @Resource
    UserServiceImpl userService;

    UserController(UserServiceImpl muSvc ) {
        this.userService=muSvc;
        this.svcContext = muSvc;
    }

    @Autowired
    OrderFeignService orderFeignService;

    @GetMapping("/getallfood")
    public String getallfood(){
        System.out.println("成功返回所有订单");
        return orderFeignService.findAll();
    }

    @Autowired
    StoreFeignService storeFeignService;

    @GetMapping("/getallorder")
    public String getallorder(){
        System.out.println("成功返回所有食品");
        return storeFeignService.GetPort().getData() + storeFeignService.findAll();
    }

    @RequestMapping("/getfood/{id}")
    public String getfood(@PathVariable("id") Integer id){
        return storeFeignService.queryById(id);

    }

    @PutMapping("/update/")
    @Operation(summary = "修改密码")
    public BaseResponse<Boolean> updatePassword( @RequestBody JSONObject jsonObject) {
        Boolean flag = userService.updatePassword(jsonObject.getLong("id"), jsonObject.getString("password"));
        log.info("用户密码修改" + flag);
        return ResultUtils.success(flag);
    }

    @PostMapping("/register")
    @Operation(summary = "注册用户")
    public BaseResponse<User> register( @RequestBody Register register ){
        return ResultUtils.success(userService.register(register));
    }

    @PostMapping("upImg")
    @Operation(summary = "插入图片")
    public BaseResponse<String> setImgByUserId( Long id, MultipartFile file ) {
        return ResultUtils.success(userService.setImgByUserId(id, file));
    }

}
